<template>
  <div v-loading="loading">
    <el-table
      :data="tableData"
      v-bind="$attrs"
      class="w-table"
      v-on="$listeners"
      @selection-change="handleSelectionChange"
    >
      <slot name="prependSlot" />
      <el-table-column v-if="selection" type="selection" width="55" align="center" />
      <el-table-column v-if="index" type="index" width="50" label="序号" align="center">
        <template slot-scope="scope">
          <span v-if="pagination && page">
            {{ (page.pageNo - 1) * page.pageSize + scope.$index + 1 }}
          </span>
          <span v-else>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :align="isDef(column.align) ? column.align : 'center'"
        :min-width="isDef(column.minWidth) ? column.minWidth : 16 * column.label.length + 20"
        :show-overflow-tooltip="isDef(column.showOverflowTooltip) ? column.showOverflowTooltip : true"
        v-bind="column"
      >
        <template #default="{ row }">
          <template v-if="column.render">
            <span v-html="column.render(row[column.prop], row)" />
          </template>
          <span v-else> {{ column.format ? column.format(row[column.prop], row) : row[column.prop] }}</span>
        </template>
      </el-table-column>
      <slot />
    </el-table>
    <div v-if="pagination && page" class="pagination-row">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="page.pageNo"
        :page-size.sync="page.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        @current-change="getList"
        @size-change="getList"
      />
    </div>
  </div>
</template>

<script>
import { isDef } from '@/utils'

export default {
  name: 'WTable',
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    tableData: {
      type: Array,
      required: true
    },
    total: {
      type: Number
    },
    // pageNo pageSize
    page: {
      type: Object
    },
    selection: {
      type: Boolean,
      default: false
    },
    index: {
      type: Boolean,
      default: false
    },
    pagination: {
      type: Boolean,
      default: true
    },
    // 表头
    columns: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      multipleSelection: []
    }
  },
  methods: {
    isDef,
    getList() {
      this.$emit('getList')
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getSelection() {
      return this.multipleSelection
    }
  }
}
</script>

<style scoped lang="scss">
.pagination-row {
  margin-top: 10px;
  text-align: right;
  ::v-deep .el-pagination {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>
